/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月3日
 */
package com.massyframework.beanskin.runtime.framework.installing;

import java.util.Objects;
import java.util.function.Consumer;

import com.massyframework.beanskin.Environment;
import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.framework.Framework;
import com.massyframework.beanskin.service.ExportServiceDescriptor;
import com.massyframework.beanskin.service.ExportServiceDescriptorBuilder;
import com.massyframework.beanskin.service.ServiceRegistration;
import com.massyframework.beanskin.service.ServiceRegistry;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.service.awaitstrategy.AwaitStrategyBuilder;
import com.massyframework.beanskin.service.awaitstrategy.AwaitStrategyRepository;
import com.massyframework.beanskin.util.ServiceLoaderUtils;

/**
 * 缺省的安装环境
 * @author huangkh
 *
 */
public final class DefaultEnvironment<T extends Module<?>> implements Environment<T> {

	private Framework framework;
	private Assembly kernel;
	private T module;
	
	/**
	 * 构造方法
	 */
	public DefaultEnvironment(Framework framework, Assembly kernel, T module) {
		this.framework = Objects.requireNonNull(framework, "\"framework\" cannot be null.");
		this.kernel = Objects.requireNonNull(kernel, "\"kernel\" cannot be null.");
		this.module = Objects.requireNonNull(module, "\"module\" cannot be null.");
	}

	@Override
	public void addAwaitStrategy(Consumer<AwaitStrategyBuilder> action) {
		AwaitStrategyRepository repo = this.kernel.getAssemblyContext().getBean(
				AwaitStrategyRepository.class.getName(), AwaitStrategyRepository.class);
		repo.addStrategy(module, action);
	}

	@Override
	public Assembly getKernelAssembly() {
		return this.kernel;
	}

	@Override
	public T getModule() {
		return this.module;
	}

	@Override
	public Framework getFramework() {
		return this.framework;
	}

	@Override
	public ServiceRegistration<?> registService(Object service, Consumer<ExportServiceDescriptorBuilder> consumer) {		
		ExportServiceDescriptorBuilder builder = 
				ServiceLoaderUtils.loadService(ExportServiceDescriptorBuilder.class, this.getClass().getClassLoader());
		consumer.accept(builder);
		ExportServiceDescriptor descriptor = builder.build();
		
		
		ServiceRepository repo = ServiceRepository.retrieveFrom(this.module);
		ServiceRegistry registry = repo.getServiceRegistry();
		return registry.register(descriptor.getExportTypes(), service, descriptor.getServiceProperties());
	}

}
